﻿/****Added By Dhanapal on Nov 25 2010*******/
function getimagename(obj) {
    var srcpath = obj.src;
    var patharray = srcpath.split("/");
    var imagename = patharray[patharray.length - 1];
    return imagename;
}

function replacenewimagepath(obj, newname) {
    var srcpath = obj.src;
    var patharray = srcpath.split("/");
    patharray[parseInt(patharray.length) - 1] = newname; //assigning new name in the last index position 
    var joinpatharr = patharray.join("/");
    return joinpatharr;
}
    

function getFirstCellValue(obj) { //To check the First Cell Value contains Numbering
    if (obj.childNodes.length > 0) {

        for (var i = 0; i < obj.childNodes.length; i++) {
            if (obj.childNodes[i].tagName == "TD") //check First Occurennce Td value
            {
                var matchvalue = obj.childNodes[i].innerHTML;
                return isNaN(parseInt(matchvalue));

            } else {

            }
        }

    }

}
    
function ExpandandCollapseSection(obj, eleid) {


    var imagename = getimagename(obj);
    var elementid = "ctl00_ContentPlaceHolder3_" + eleid;
    if (document.getElementById(elementid) != null) {
        var parentTableobj = document.getElementById(elementid).parentNode.parentNode;
        var childNodesobj = parentTableobj.childNodes;
        var allowflag = "false";
        if (childNodesobj.length > 0) {
            for (var z = 0; z < childNodesobj.length; z++) {
                if (childNodesobj[z].tagName == "TBODY") {
                    var tableBodyObj = childNodesobj[z];
                    allowflag = "true";
                    break;
                }
            }
            if (allowflag == "false") { // To restrict Further operation if Tbody doesnot exist
                return false;
            }

            var tableBodyChildNodes = tableBodyObj.childNodes;
            
            var clickedSectionId = elementid;
            var startflag = false;
            for (var i = 0; i < parseInt(tableBodyChildNodes.length); i++) {
                //var elementid = "ctl00_ContentPlaceHolder3_" + eleid;
                if (tableBodyChildNodes[i].tagName == "TR") {

                    var iterateTrId = tableBodyChildNodes[i].id;

                   
                    if (iterateTrId != "" && clickedSectionId == iterateTrId) {
                       
                        if (i < parseInt(tableBodyChildNodes.length)) {
                           
                            var returnval = "";
                            if (imagename.toLowerCase() == "expand3.gif") {
                                obj.src = replacenewimagepath(obj, "collapse3.GIF");
                                obj.title = "Collapse";
                                //obj.title = "collapse";
                                ExpandandhideSectionChild(tableBodyChildNodes, i, "SHOW" ,clickedSectionId);
                                break; // come out of the loop;
                            } else {
                                obj.src = replacenewimagepath(obj, "expand3.GIF");
                                obj.title = "Expand";
                                ExpandandhideSectionChild(tableBodyChildNodes, i, "HIDE",clickedSectionId);
                                break; // come out of the loop;
                            }
                        }
                       
                    }
                }
            } // End of If "TR" condition
        }
    }
    
}
    /*****End *******************************/
    
function ExpandandhideSectionChild(ChildObj, startindex, condval,clickedSectionId) {
    for (var i = parseInt(startindex) + 1; i < ChildObj.length; i++) {
        if (ChildObj[i].tagName == "TR") {
            var issameSection = getFirstCellValue(ChildObj[i]); // This is for MultiRecPage 
            if (issameSection == false) {
                //return;
            }
            var iterateTrId = ChildObj[i].id;
            //alert(clickedSectionId + "---" + iterateTrId);
            //ctl00_ContentPlaceHolder3_pe_sectionheader___MB_EMPTY_SECTION_1
            //ctl00_ContentPlaceHolder3_pe_sectionheader___MB_EMPTY_SECTION_1__Child7
            //alert(clickedSectionId);
            //if (iterateTrId.indexOf("ctl00_ContentPlaceHolder3_pe_sectionheader") == "0"  ) {
            if (iterateTrId.indexOf(clickedSectionId) != "0"  ) {
                return;
            } else {
                if (condval == "SHOW") {
                    ChildObj[i].style.display = '';
                } else {
                    ChildObj[i].style.display = 'none';
                }
            }
        } //end of if Tr
    }

}